package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.ListSong;
import com.jinzhuo.music.domain.SongList;
import com.jinzhuo.music.service.ListSongService;
import com.jinzhuo.music.service.SongListService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ListSong")
public class ListSongController {
    @Autowired
    private ListSongService listSongService;

    /**
     * 添加
     */
    @RequestMapping(value = "addListSong",method = RequestMethod.POST)
    public Object addListSong(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        //保存到歌单下的歌曲的对象中
        ListSong listSong = new ListSong();
        listSong.setSongId(Integer.parseInt(songId));
        listSong.setSongListId(Integer.parseInt(songListId));
        boolean flag = listSongService.addListSong(listSong);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"添加成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"添加失败");
        }
        return object;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "updateListSong",method = RequestMethod.POST)
    public Object updateListSong(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        //保存到歌单歌曲的对象中
        ListSong listSong = new ListSong();
        listSong.setId(Integer.parseInt(id));
        listSong.setSongId(Integer.parseInt(songId));
        listSong.setSongListId(Integer.parseInt(songListId));
        boolean flag = listSongService.updateListSong(listSong);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"修改成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"修改失败");
        }
        return object;
    }

    /**
     * 删除(抛弃该方法)
     */
    @RequestMapping(value = "deleteListSong",method = RequestMethod.POST)
    public Object deleteListSong(HttpServletRequest request) {
//        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        boolean flag = listSongService.deleteListSong(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据歌曲id和歌单id删除
     */
    @RequestMapping(value = "deleteBySongIdAndSongListId",method = RequestMethod.POST)
    public Object deleteBySongIdAndSongListId(HttpServletRequest request) {
//        JSONObject object = new JSONObject();
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        boolean flag = listSongService.deleteBySongIdAndSongListId(Integer.parseInt(songId),Integer.parseInt(songListId));
        return flag;
    }

    /**
     * 据主键查询
     */
    @RequestMapping(value = "getListSong",method = RequestMethod.POST)
    public Object getListSong(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        ListSong listSong = listSongService.getListSong(Integer.parseInt(id));
        return listSong;
    }

    /**
     * 查询歌单下所有歌曲
     */
    @RequestMapping(value = "queryAllListSong",method = RequestMethod.POST)
    public Object queryAllListSong(HttpServletRequest request) {
//        String id = request.getParameter("id").trim();
        return listSongService.queryAllListSong();
    }

    /**
     * 根据歌单id查询歌曲
     */
    @RequestMapping(value = "querySongBySongListId",method = RequestMethod.POST)
    public Object querySongBySongListId(HttpServletRequest request) {
        String songListId =request.getParameter("songListId").trim();
        List<ListSong> listSongs = listSongService.querySongBySongListId(Integer.parseInt(songListId));
        return listSongs;
    }


}
